package hello;

import java.util.*;

import org.hibernate.*;
import persistence.*;

public class HelloWorld
{
	public static void main(String[] args) 
	{
		//
		Session session=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=session.beginTransaction();
		Message message=new Message("Hello World!");
		Long msgId=(Long)session.save(message);
		tx.commit();
		session.close();
		
		//
		Session thirdSession=HibernateUtil.getSessionFactory().openSession();
		Transaction thirdtx=thirdSession.beginTransaction();
		
		message=(Message)thirdSession.get(Message.class,msgId);
		message.setText("Greeting Earthling");
		
		message.setWei("no mapped!"); //no mapping
		//getParent(message.getClass());
		
		message.setNextMessage(new Message("Take me to your leader(please)"));
		thirdtx.commit();
		thirdSession.close();
		
		//show all
		Session newSession=HibernateUtil.getSessionFactory().openSession();
		Transaction newtx=newSession.beginTransaction();
		List messages=newSession.createQuery("from Message m order by m.text asc").list();
		System.out.println(messages.size()+" message(s) found:");
		for(Iterator it=messages.iterator();it.hasNext();)
		{
			Message loadedMsg=(Message)it.next();
			Message nextMsg=loadedMsg.getNextMessage();
			/*if(nextMsg!=null)
				getParent(nextMsg.getClass());*/
			System.out.println(loadedMsg.getText()+"<--->");
		}
		newtx.commit();
		newSession.close();
		
		HibernateUtil.stat();
		HibernateUtil.shutdown();
	}
	
	public static void getParent(Class clazz)
	{
		Class parent=clazz.getSuperclass();
	  if(parent !=null)
	  {
			System.out.println("###"+parent.getName());
			getParent(parent);
		}
	}
}